package com.example.MastAndSlave.controller.MAS;

import com.example.MastAndSlave.domain.User;
import com.example.MastAndSlave.service.MasService;
import org.junit.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;

@Controller
@RequestMapping("/Mas")
public class MasController {

    @Autowired
    private MasService masService;

    @GetMapping("/selectUser")
    @ResponseBody
    public String selectUser(){
        User user =  masService.selectUserById(1);
        return user.toString();
    }

    @GetMapping("/insertUser")
    @ResponseBody
    public String insert(){
        User u = new User();
        u.setAge(11);
        u.setName("测试插入");
        int result = masService.insertUser(u);
        return result+"";
    }


    @Test
    public void foreachQuery() {
        int count = 0;
        Map<String, String> map = new HashMap<String, String>();
        for (int i = 0; i < 100000; i++) {
            map.put("key=" + i, "value=" + i);
        }

        long startTimeFor = System.currentTimeMillis();
        for (Map.Entry<String, String> entry : map.entrySet()) {
            System.out.println(entry.getKey());
            System.out.println(entry.getValue());
            count++;
        }
        System.out.println("查询记录数=" + count);
        System.out.println("增强for查询耗时：" + (startTimeFor - System.currentTimeMillis()));
    }

    @Test
    public void iteratorQuery() {
        int count = 0;
        Map<String, String> map = new HashMap<String, String>();
        for (int i = 0; i < 1000000; i++) {
            map.put("key=" + i, "value=" + i);
        }

        Iterator<Map.Entry<String, String>> iterator = map.entrySet().iterator();
        long startTimeIterator = System.currentTimeMillis();
        while (iterator.hasNext()) {
            String key = iterator.next().getKey();
            String value = iterator.next().getValue();
            System.out.println(key);
            System.out.println(value);
            count++;
        }
        System.out.println("查询记录数=" + count);
        System.out.println("迭代器Iterator查询耗时：" + (startTimeIterator - System.currentTimeMillis()));
    }
}
